package cn.uniqueinfo.dp.service.base;

import cn.uniqueinfo.dp.mapper.BaseMapper;
import cn.uniqueinfo.dp.vo.BaseVO;
import cn.uniqueinfo.dp.vo.EasyuiTableResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-04 11:46:46
 * @description:服务实现类
 */
public class BaseServiceImpl<T extends BaseVO> implements IBaseService<T> {

    protected BaseMapper baseMapper;

    @Override
    public Long insert(T item) {
        return this.baseMapper.insert(item);
    }

    @Override
    public Integer delete(T item) {
        return this.baseMapper.delete(item);
    }

    @Override
    public Integer deleteById(Long id) {
        T item = (T) new BaseVO();
        item.setId(id);
        return this.baseMapper.delete(item);
    }

    @Override
    public Integer deleteByIds(String ids) {
        return this.baseMapper.deleteByIds(ids);
    }

    @Override
    public Integer update(T item) {
        return this.baseMapper.update(item);
    }

    @Override
    public T findOne(T item) {
        return (T) this.baseMapper.findOne(item);
    }

    @Override
    public T findOneById(Long id) {
        T item = (T) new BaseVO();
        item.setId(id);
        return (T) this.baseMapper.findOne(item);
    }

    @Override
    public List<T> findAll(T item) {
        return this.baseMapper.findAll(item);
    }

    @Override
    public EasyuiTableResult<T> findPage(T item, Integer pageNum, Integer pageSize) {
        EasyuiTableResult<T> result = new EasyuiTableResult<>();
        PageHelper.startPage(pageNum, pageSize, true);
        List<T> rows = this.baseMapper.findPage();
        PageInfo<T> pageInfo = new PageInfo<>(rows);
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }
}
